<template>
  <div class="demo-image__error">
    <!-- <div class="block">
      <span class="demonstration">Default</span>
      <el-image />
    </div> -->
    <div class="block">
      <!-- <span class="demonstration">Custom</span> -->
      <el-image :src="imageSrcData">
        <template #error>
          <div class="image-slot">
            <el-icon><icon-picture /></el-icon>
          </div>
        </template>
      </el-image>
    </div>
    <div class="block">
      <el-button @click="refreshCode">
        <span class="demonstration">刷新验证码</span>
        <el-icon><RefreshRight /></el-icon
      ></el-button>
    </div>
    <div class="block">
      <el-input placeholder="请输入验证码进行验证" class="verify_code" v-model="inputCode"/>
      <el-button type="primary" class="verify_button" @click="verifyCode">验证</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Picture as IconPicture, RefreshRight } from "@element-plus/icons-vue"
import { onMounted, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { getSimpleCaptchaInfo, verifySimpleCaptchaInfo } from '@/api/captcha'

// 数据data
let imageSrcData: any = ref("");
// input 接受的验证码
let inputCode: any = ref("");
// 请求token
let simpleToken: any = ref("");

// 获取验证码信息
const getSimpleInfo = () => {
  getSimpleCaptchaInfo().then((response) => {
    imageSrcData.value = response.base64ImageString;
    simpleToken.value = response.token;
    ElMessage({
      type: 'success',
      message: "请求成功",
      showClose: true,
    })
  }).catch((error) => {
    console.log(error)
    ElMessage({
      showClose: true,
      message: 'Oops, this is a error message.' + String(error),
      type: 'error',
    })
  })
}

onMounted(() => {
  getSimpleInfo();
})

// 刷新验证码
const refreshCode = () => {
  getSimpleInfo();
};

// 验证Code
const verifyCode = () => {
  verifySimpleCaptchaInfo({
    "token": simpleToken.value,
    "code": inputCode.value
  }).then((response) => {
    console.log(response)
    if (response.status == 200){
      ElMessage({
        type: 'success',
        message: "验证成功",
        showClose: true,
      })
      getSimpleInfo();
      inputCode.value = ""
    }else{
      ElMessage({
        type: 'error',
        message: "验证失败",
        showClose: true,
      })
      inputCode.value = ""
    }
  })
}

</script>

<style lang="scss" scoped>
.demo-image__error .block {
  padding: 30px 0;
  text-align: center;
  display: inline-block;
  width: 100%;
  box-sizing: border-box;
  vertical-align: top;
}
.demo-image__error .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-right: 10px;
}
.demo-image__error .el-image {
  padding: 0 5px;
  max-width: 600px;
  max-height: 200px;
  width: 100%;
  height: 200px;
}

.demo-image__error .image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: var(--el-fill-color-light);
  color: var(--el-text-color-secondary);
  font-size: 30px;
}
.demo-image__error .image-slot .el-icon {
  font-size: 30px;
}

.demo-image__error .block {
  .verify_code {
    width: 400px;
    text-align: center;
  }
  .verify_button {
    margin: 0 20px;
  }
}
</style>
